﻿using System;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Wpf.Mvvm.ViewModels
{
    using Songhay.Models;

    /// <summary>
    /// View Model for Application Message Page
    /// </summary>
    public class ApplicationMessagePageViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationMessagePageViewModel"/> class.
        /// </summary>
        public ApplicationMessagePageViewModel()
        {
            if(base.IsInDesignMode)
            {
                this.ApplicationMessage = "Design-time application message.";
                this.PageTitle = "Page Title";
                this.IsErrorMessage = true;
            }
            else
            {
                //Receive MvvmLight message:
                Messenger.Default.Register(this,
                    new Action<GenericMessage<ClientMessage<Page>>>(
                    message =>
                    {
                        var page = message.Content.ClientView;
                        this.ApplicationMessage = message.Content.DisplayText;
                        this.PageTitle = page.Title;
                        this.IsErrorMessage = (message.Content.ForClientEvent == ClientEvent.NavigationFailed);
                    }));
            }
        }

        /// <summary>
        /// Gets or sets the application message.
        /// </summary>
        /// <value>The application message.</value>
        public string ApplicationMessage
        {
            get { return _applicationMessage; }
            set
            {
                this._applicationMessage = value;
                base.RaisePropertyChanged("ApplicationMessage");
            }
        }

        /// <summary>
        /// Gets or sets the page title.
        /// </summary>
        /// <value>The page title.</value>
        public string PageTitle
        {
            get { return _pageTitle; }
            set
            {
                this._pageTitle = value;
                base.RaisePropertyChanged("PageTitle");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is error message.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is error message; otherwise, <c>false</c>.
        /// </value>
        public bool IsErrorMessage
        {
            get { return _isErrorMessage; }
            set
            {
                this._isErrorMessage = value;
                base.RaisePropertyChanged("IsErrorMessage");
            }
        }

        string _applicationMessage;
        string _pageTitle;
        bool _isErrorMessage;
    }
}
